<?php
/**
 * beimuaihui System
 * Copyright(c) 2011-2020 beimuaihui.
 * @license    http://www.gnu.org/licenses/gpl.html     This software Under GPL V3 License 
 * beimuaihui@gmail.com
 * http://code.google.com/p/beimuaihui/
 * $Id: MultilangInfo.php 197 2011-02-18 12:45:33Z beimuaihui $
 */


Zend_Loader::loadClass("Baogg_Db_Table");

class MultilangInfo extends Baogg_Db_Table {
	/**
	 * The default table name 
	 */
	protected $_name = 'multilang_info';
	protected $_primary=array('multilang_table_id','key_id','language_id');
	function __construct($_fk='') {
		global $db;
		parent::__construct ();
		$this->db = $db;
		//$this->params=getUrlParams();
	}
	
	/**
	 * get a table's multilanguage content
	 * @author bob
	 * @param string $table_name, primary key is $table_name."_id"
	 * @param mixed $where
	 * @param string $order
	 * @param int $language_id
	 * @return array
	 */
	function getDataLang($table_name, $where, $order, $language_id,$pager='',$fk='') {
		global $config;
		$fk=$fk?$fk:"{$table_name}_id";
		if (! $table_name) {
			throw new Exception ( "Please give a table name!" );
		}
		$language_id = is_numeric ( $language_id ) ? $language_id : $config['lang']['default'];
				
		$select = $this->db->select ();
		$select->from ( "{$table_name} as a", '*' );
		$select->join ( "{$this->_name} as b", "a.{$fk}=b.key_id  and b.language_id={$language_id}", array('content','value') );
		$select->join ( "multilang_table as  c", "b.multilang_table_id=c.multilang_table_id and  c.table_name='{$table_name}'  and c.state=1", "multilang_table_id" );
		if ($where) {
			if(is_array($where)){
				$where=implode(" and ",$where);
			}
			$select->where ( $where );
		}
		if ($order) {
			$select->order ( $order );
		}
		
		try{
			$rows = $this->db->fetchAll ( $select );
		}catch(Exception $e){
			echo $e->getMessage(); //$select;//
		}
		if($pager && is_numeric($pager->_offsize) &&  is_numeric($pager->_limit)){
			$pager->_rowCount = count($rows);
			$rows = array_slice($rows,$pager->_offsize,$pager->_limit);
		}
		//$rows=array_values($rows);
		return $rows;
	}
	
	function insert($arr) {
		$arr['updated_by']= isset($arr['$updated_by'])?$arr['$updated_by']:$_SESSION['s_admin_user_id'];
		$arr['updated_time']	=$arr['updated_time']?$arr['updated_time']:date ( "Y-m-d H:i:s" );
		
		$tableName 			= $this->_name;
		$arrInfo 			= filterColumn ( $tableName, $arr );
		$rows_affected 		= $this->db->insert ( $tableName, $arrInfo );
		return $rows_affected;
	}
	
	function delete($params) {
		global  $config;
		if (! is_array ( $params ) || ! $params ['key_id'] || ! $params ['multilang_table_id']) {
			return null;
		}
		
		
		$languageId = $params ['language_id']?$params ['language_id']:$config['lang']['default'];
		$table_id = is_array ( $params ['multilang_table_id'] ) ? $params ['multilang_table_id'] [0] : $params ['multilang_table_id'];
		$ids = ( array ) $params ['key_id'];
		
		$dbFlag = 1;
		foreach ( $ids as $id ) {
			$where=array();			
			$where [] = $this->db->quoteInto ( 'key_id = ?', $id );
			$where [] = $this->db->quoteInto ( 'multilang_table_id = ?', $table_id );
			$where[] = $this->db->quoteInto ( 'language_id = ?', $languageId );
			$rows_affected = $this->db->delete ( $this->_name, $where );
			$dbFlag = $dbFlag && $rows_affected;
		}
		return $dbFlag;
	}
	
	
	function update($arr) {
		$arr['updated_by']= isset($arr['$updated_by'])?$arr['$updated_by']:$_SESSION['s_admin_user_id'];
		$arr['updated_time']	=$arr['updated_time']?$arr['updated_time']:date ( "Y-m-d H:i:s" );
		
		$tableName = $this->_name;
		$_arr = filterColumn ( $tableName, $arr );
		$where [] = $this->db->quoteInto ( "multilang_table_id = ?", $_arr ["multilang_table_id"] );
		$where [] = $this->db->quoteInto ( "key_id = ?", $_arr ["key_id"] );
		$where [] = $this->db->quoteInto ( "language_id = ?", $_arr ["language_id"] );
		$rows_affected = $this->db->update ( $tableName, $_arr, $where );

		return $rows_affected;
	}
	
	function getInfoName(array $arr) {
		$select = $this->db->select ();
		$select->from ( $this->_name, '*' );
		
		$where [] = $this->db->quoteInto ( "multilang_table_id = ?", $arr ["multilang_table_id"] );
		$where [] = $this->db->quoteInto ( "key_id = ?", $arr ["key_id"] );
		$where [] = $this->db->quoteInto ( "language_id = ?", $arr ["language_id"] );
		$select->where(implode($where," and "));

		$rows = $this->db->fetchAll ( $select );
		$content=$rows [0] ['content'];
		$content=!$content && $rows? "":$content;
		return $content;
	}
	function getTableIdByName($name){
		$select = $this->db->select ();
		$select->from ( "multilang_table", '*' );
		
		$where[]  = $this->db->quoteInto ( "table_name = ?", $name );
		//$where[]  =  "state = 1" ;
		$select->where(implode($where," and "));
		$rows = $this->db->fetchAll ( $select );
		
		if(!$rows){
			$this->db->insert("multilang_table",array("table_name"=>$name));
			$tableId=$this->db->lastInsertId();
		}else{
			$tableId=$rows [0] ['multilang_table_id'];
		}
		return $tableId;
	}
	function getSelects($result,$pk='',$parentKey='',$topId=0) {
		//$result=$this->getAll();
		$pk=$pk?$pk:"module_permission_id";
		$parentKey=$parentKey?$parentKey:"module_permission_pid";
		
		/*foreach ( ( array ) $result as $key => $value ) {
			if ($value [$parentKey] == 0) {
				$module_arr [$value [$pk]] = "&nbsp;|-" . $value ['content'] . "";
			} else {
				$module_arr [$value [$pk]] = "&nbsp;&nbsp;&nbsp;&nbsp;|--" . $value ['content'];
			}
		}*/
		foreach($result as $value){
			$map[$value[$parentKey]][]=$value;
		}
		foreach((array)($map[$topId]) as $parent){
			$module_arr[$parent[$pk]]= "&nbsp;|-" . $parent ['content'] . "";
			foreach((array)($map[$parent[$pk]]) as $child){
				$module_arr[$child[$pk]]= "&nbsp;&nbsp;&nbsp;&nbsp;|--" . $child ['content'] . "";
			}
		}
		return $module_arr;
	}
	
	function checkExistContent($arr=array(),$condition=array()){
				
		if(!$arr['table_id']){
			$arr['table_id']=$this->getTableIdByName($arr['table_name']);
		}
		if(!$arr['content'] || !$arr['table_name']){
			return 0;
		}
		$select = $this->db->select ();
		$select->from ( "multilang_info", '*' );
		
		if($condition){
			$where=$condition;
		}
		$where[]=$this->db->quoteInto ( "multilang_table_id = ?", $arr['table_id'] );
		$where[]=$this->db->quoteInto ( "content = ?", $arr['content'] );		
		
		$select->where(implode($where," and "));
		$rows = $this->db->fetchAll ( $select );
		return $rows?1:0;
	}
}
